<template>
  <a-modal
    title="编辑发布管理"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
<!--        <a-form-item-->
<!--          label="图片路径"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          has-feedback-->
<!--        >-->
<!--          <a-input placeholder="请输入图片路径" v-decorator="['imagePath', {rules: [{required: true, message: '请输入图片路径！'}]}]" />-->
<!--        </a-form-item>-->
        <a-form-item
          label="列表名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入列表名称" v-decorator="['lineName', {rules: [{required: true, message: '请输入列表名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="分辨率"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入分辨率" v-decorator="['distinguishability', {rules: [{required: true, message: '请输入分辨率！'}]}]" />
        </a-form-item>
        <a-form-item
          label="终端类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入终端类型" v-decorator="['terminalType', {rules: [{required: true, message: '请输入终端类型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="播放时长(秒)"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入播放时长" v-decorator="['playTime', {rules: [{required: true, message: '请输入播放时长！'}]}]" />
        </a-form-item>
        <a-form-item
          label="声音输出"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入声音输出" v-decorator="['voiceOutput', {rules: [{required: true, message: '请输入声音输出！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { pushManageEdit } from '@/api/modular/system/pushManageManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              imagePath: record.imagePath,
              lineName: record.lineName,
              distinguishability: record.distinguishability,
              terminalType: record.terminalType,
              playTime: record.playTime,
              voiceOutput: record.voiceOutput,
              createTime: record.createTime
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            pushManageEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
